package config
import (
    "io/ioutil"
    "encoding/json"
    "github.com/name5566/leaf/log"
)
        
// 一些配置选项
type SettingConfig struct {
	Key	string
	Value	string
}

var SettingConfigLoader struct {
    Data map[string]*SettingConfig    
}

func GetSettingConfig(key string, args...bool) *SettingConfig {
    if value, ok := SettingConfigLoader.Data[key]; ok {
        return value
    } else {
        if len(args) > 0 && args[0]  == true {
            log.Error("未找到该key,  SettingConfigLoader  %v", key)
        }
        return nil
    }
}

func init() {
    buffer, err := ioutil.ReadFile("cfg/Setting.json")
    if err != nil {
        log.Fatal("%v", err)
    }


    var data []*SettingConfig
    err = json.Unmarshal(buffer, &data)
    if err != nil {
        log.Fatal("%v", err)
    }

    SettingConfigLoader.Data = make(map[string]*SettingConfig)

    for _, v := range data {
        SettingConfigLoader.Data[v.Key] = v
    }
}

